package net.slavics.maps;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import net.slavics.images.Images;
import net.slavics.maps.ifc.IMapa;
import org.apache.log4j.Logger;

/**
 *
 * @author slavic01
 * @version $Revision$
 * $LastChangedDate$ $Author$
 */
public class CellMap {

    private static Logger _log = Logger.getLogger(CellMap.class);
    private Component owner;
    private BufferedImage img;
    public boolean needUpdate = true;
    private int imgWidth;
    private int imgHeight;
    private Point screen=new Point();
    private Point sektor;

    /**
     * Konstruktor vyžaduje jako parametr grafického vlastníka (rodiče) objektu,
     * tedy nějakou Componentu na které spočívá.
     * @param owner Vlastník
     * @param iMapa Zobrazované Mapy
     * @param sektor Mapy
     */
    public CellMap(GridMap owner, IMapa iMapa, Point sektor) {
        synchronized (sektor) {
            this.owner = owner;
            this.imgWidth = iMapa.getWidth();
            this.imgHeight = iMapa.getHeight();
            this.img = Images.getTestImage(imgWidth, imgHeight);
            Images.setName(img, sektor.toString());
            this.sektor = sektor;
            (new Thread(new ThreadMap(this, iMapa, sektor))).start();
        }
    }

    public void paint(Graphics g) {
        _log.trace("Překreslení " + needUpdate + " [" + screen.x + "," + screen.y + "]");
        if (!needUpdate) {
            Rectangle toDraw = g.getClipBounds().intersection(new Rectangle(screen.x, screen.y, imgWidth, imgHeight));
            if (!toDraw.isEmpty()) {
                g.drawImage(img, screen.x, screen.y, imgWidth, imgHeight, null);
            }
        } else {
            g.drawImage(img, screen.x, screen.y, imgWidth, imgHeight, null);
            needUpdate = false;
        }
    }

    /**
     * Nakreslí mapu na zvolené souřadnice
     * @param screenXY
     */
    public synchronized void show(Point screenXY) {
        _log.debug("Show ["+screenXY.x+","+screenXY.y+"]");
        screen=new Point(screenXY);
        needUpdate = true;
        //owner.repaint();
    }
    @Override
    public String toString() {
        return "Sektor [" + sektor.x + "," + sektor.y + "] na souřadnicích [" + screen.x + "," + screen.y + "]";
    }

    /**
     * Vrací pozici mapy
     * @return pozice
     */
    public Point getSektor() {
        return sektor;
    }
    public class ThreadMap implements Runnable {

    GridMap gMap;
        IMapa iMapa;
        Point sektor;

        public ThreadMap(CellMap cMap, IMapa iMapa, Point sektor) {
            this.gMap = (GridMap) cMap.owner;
            this.iMapa = iMapa;
            this.sektor = sektor;
        }

        public void run() {
            String name = sektor.x + "-" + sektor.y;
            _log.info("Thread " + name + " start");
            //gMap.mc.addProgressBar(name, 1);
            img = iMapa.getMap(sektor);
            //gMap.mc.removeProgressBar(name);
            _log.info("Thread " + name + " stop");
            gMap.repaint();
        }
        
    }
} 
	
